<?php

class ModelePDO {
   
   

// Attributs utiles pour la connexion
   protected static $serveur = MySqlConfig::SERVEUR;
   protected static $base = MySqlConfig::BASE;
   protected static $utilisateur = MySqlConfig::UTILISATEUR;
   protected static $passe = MySqlConfig::MOT_DE_PASSE;
// Attributs utiles pour la manipulation PDO de la BDD
   /**
    *
    * @var type PDO
    */
   protected static $pdoCnxBase = null;

   /**
    *
    * @var type PDOStatment
    */
   protected static $pdoStResults = null;
   protected static $requete = '';
   protected static $resultat = null;

   /**
    *
    * @var type Exception
    */
   protected static $messageException = "Impossible d'accéder à votre requête.";

   /**
    *
    * @var type Exception
    */
   protected static $messageExceptionSuppression = "Valeur introuvable dans la base de données.";

   protected static function seConnecter() {
      if (is_null(self::$pdoCnxBase)) {
         try {
            self::$pdoCnxBase = new PDO('mysql:host=' . self::$serveur . ';dbname=' . self::$base, self::$utilisateur, self::$passe);
            self::$pdoCnxBase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            self::$pdoCnxBase->query("SET CHARACTER SET utf8");
         } catch (Exception $e) {
            echo 'Erreur : ' . $e->getMessage() . '<br />';
            echo 'Code : ' . $e->getCode();
            die();
         }
      }
   }

   protected static function seDeconnecter() {
      self::$pdoCnxBase = null;
   }

    /**
     * @param $table
     * @return bool|News[]
     */
   protected static function getLesTuples($table) {
      try {
         self::seConnecter();

         self::$requete = "SELECT * FROM " . $table;
         self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
         self::$pdoStResults->execute();
         self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
         self::$pdoStResults->closeCursor();

         if (self::$resultat == true) {
            return self::$resultat;
         } else {
            return false;
            throw new Exception(self::$messageException);
         }
      } catch (Exception $e) {
         echo 'Erreur : ' . $e->getMessage() . '<BR>';
         die();
      }
   }

    protected static function getLesTupleByChamp($table, $nomChamp) {
        try {
            self::seConnecter();

            self::$requete = "SELECT " . $nomChamp . " FROM " . $table;
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);

            self::$pdoStResults->closeCursor();
            if (self::$resultat == true) {
                return self::$resultat;
            } else {
                return false;
                throw new Exception(self::$messageException);
            }
        } catch (Exception $e) {
            echo 'Erreur : ' . $e->getMessage() . '<BR>';
            die();
        }
    }

    protected static function getTupleByChamp($table, $nomChamp, $valeurChamp) {
        try {
            self::seConnecter();

            self::$requete = "SELECT * FROM " . $table . " WHERE " . $nomChamp . " = " . $valeurChamp;
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);

            self::$pdoStResults->closeCursor();
            if (self::$resultat == true) {
                return self::$resultat;
            } else {
                return false;
                throw new Exception(self::$messageException);
            }
        } catch (Exception $e) {
            echo 'Erreur : ' . $e->getMessage() . '<BR>';
            die();
        }
    }

    protected static function getLesTupleByChampDistinct($table, $nomChamp) {
        try {
            self::seConnecter();

            self::$requete = "SELECT DISTINCT " . $nomChamp . " FROM " . $table;
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);

            self::$pdoStResults->closeCursor();
            if (self::$resultat == true) {
                return self::$resultat;
            } else {
                return false;
                throw new Exception(self::$messageException);
            }
        } catch (Exception $e) {
            echo 'Erreur : ' . $e->getMessage() . '<BR>';
            die();
        }
    }

   protected static function getPremierTupleByChamp($table, $nomChamp, $valeurChamp) {
      try {
         self::seConnecter();

         self::$requete = "SELECT * FROM " . $table . " WHERE " . $nomChamp . "=:valeurChamp";
         self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
         self::$pdoStResults->bindParam(':valeurChamp', $valeurChamp);
         self::$pdoStResults->execute();
         self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);

         self::$pdoStResults->closeCursor();
         if (self::$resultat == true) {
            return self::$resultat;
         } else {
            return false;
            throw new Exception(self::$messageException);
         }
      } catch (Exception $e) {
         echo 'Erreur : ' . $e->getMessage() . '<BR>';
         die();
      }
   }

   protected static function supprimerTupleByChamp($table, $nomChamp, $valeurChamp) {
      try {
         self::seConnecter();
         if (self::getPremierTupleByChamp($table, $nomChamp, $valeurChamp) == false) {
            return false;
            throw new Exception(self::$messageExceptionSuppression);
         } else {
            self::$requete = "DELETE FROM " . $table . " WHERE " . $nomChamp . "=:valeurChamp";
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->bindParam(':valeurChamp', $valeurChamp);
            self::$pdoStResults->execute();
            return true;
         }
      } catch (Exception $e) {
         echo 'Erreur : ' . $e->getMessage() . '<BR>';
         die();
      }
   }

   protected static function modifierTupleByChamp($table, $nomChamp, $valeurChamp, $id) {
      try {
         self::seConnecter();
         if (self::getPremierTupleByChamp($table, "id", $id) == false) {
            return false;
            throw new Exception(self::$messageException);
         } else {
            self::$requete = "UPDATE " . $table . " SET " . $nomChamp . "=:valeurChamp WHERE id = " . $id;
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->bindParam(':valeurChamp', $valeurChamp);
            self::$pdoStResults->execute();
            return true;
         }
      } catch (Exception $e) {
         echo 'Erreur : ' . $e->getMessage() . '<BR>';
         die();
      }
   }

    protected static function insert($table,$proprietes,$valeures){
        if (count($proprietes) != count($valeures)){
            throw new Exception("ATTENTION T'AS PAS TOUT BIEN REMPLIS! BIIIICHE!");
        }
        else{
            self::seConnecter();
            $requeteINSERT = "INSERT INTO $table (";

            //Gestion de l'ajout de chaque propriété de l'array de paramètre
            $firstProp = 0;
            foreach ($proprietes as $unePropriete) {
                if ($firstProp == 0) {
                    $requeteINSERT = $requeteINSERT . $unePropriete;
                    $firstProp = 1;
                } else {
                    $requeteINSERT = $requeteINSERT . "," . $unePropriete;
                }
            }

            $requeteINSERT = $requeteINSERT.") VALUES (";

            //Gestion de l'ajout de chaque VALUE
            $firstValue = 0;
            foreach ($valeures as $uneValeure) {
                if (is_string($uneValeure)) {
                    $uneValeure = "'" . $uneValeure . "'";
                }
                if ($firstValue == 0) {
                    $requeteINSERT = $requeteINSERT . $uneValeure;
                    $firstValue = 1;
                } else {
                    $requeteINSERT = $requeteINSERT . "," . $uneValeure;
                }
            }

            $requeteINSERT = $requeteINSERT.")";

            self::$requete = $requeteINSERT;

            try{
                self::$PDOresults= self::$pdoCnxBase->prepare(self::$requete);
                self::$PDOresults->execute();
                return true;
            }
            catch(PDOException $e)
            {
                echo 'Erreur : '.$e->getMessage();
                echo 'N° : '.$e->getCode();
                return false;
            }
        }
    }

    protected static function select($requete) {
        self::seConnecter();

        try {
            self::$requete = $requete;
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
            self::$pdoStResults->closeCursor();
            if (self::$resultat == false) {
                return false;
                throw new Exception("Erreur survenue lors de la requête.");
            } else {
                return self::$resultat;
            }
        } catch (Exception $e) {
            echo 'Erreur : ' . $e->getMessage();
            die();
        }
    }

    /**
     * Execute une requête de type INSERT / DELETE ou UPDATE.
     * @param type $requete
     * @return boolean
     */
    protected static function execute($requete) {
        self::seConnecter();

        self::$requete = $requete;
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        return true;
    }
}
?>
